package cn.zwz.insurance.serviceimpl;

import cn.zwz.data.entity.User;
import cn.zwz.data.entity.UserRole;
import cn.zwz.data.service.IUserRoleService;
import cn.zwz.data.service.UserService;
import cn.zwz.data.serviceimpl.IUserServiceImpl;
import cn.zwz.insurance.mapper.RosterMapper;
import cn.zwz.insurance.entity.Roster;
import cn.zwz.insurance.service.IRosterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 参保人员 服务层接口实现
 * @author 郑为中
 */
@Slf4j
@Service
@Transactional
public class IRosterServiceImpl extends ServiceImpl<RosterMapper, Roster> implements IRosterService {

    @Autowired
    private RosterMapper rosterMapper;
    @Autowired
    private IUserServiceImpl userService;
    @Autowired
    private IUserRoleService userRoleService;

    @Override
    @Transactional
    public void saveNewUser(Roster roster) {
        User user  = new User();
        user.setCreateBy(roster.getCreateBy());
        user.setCreateTime(roster.getCreateTime());
        user.setAvatar("https://wx.qlogo.cn/mmhead/PiajxSqBRaEKjc639z2lRF4DqweNw2aDRLbAL7yLlSgrdZeKBCwLHDQ/0");
        user.setMobile(roster.getMobile());
        user.setNickname(roster.getName());
        user.setPassword("$2a$10$pQkHlBEiHaOBwf3xc7FUMe1AugtVae9lqjfJPqfZ55WOhz93qUfhK");
        user.setSex(roster.getSex());
        user.setStatus(0);
        user.setType(0);
        user.setUsername(roster.getName());
        user.setDelFlag(0);
        user.setDepartmentId("1589203547386417152");
        user.setDepartmentTitle("参保人员");
        user.setId(String.valueOf(System.currentTimeMillis()));
        userService.saveOrUpdate(user);
        //设置角色
        UserRole userRole = new UserRole();
        userRole.setRoleId("1589203739284213760");
        userRole.setUserId(user.getId());
        userRole.setDelFlag(0);
        userRole.setCreateBy(roster.getCreateBy());
        userRole.setCreateTime(roster.getCreateTime());
        userRoleService.save(userRole);
        roster.setUserId(user.getId());
        rosterMapper.insert(roster);
    }
}
